package base_02_clone;

import java.util.StringJoiner;

/**
 * 实现Cloneable接口，浅拷贝
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/2/8 18:13
 */
public class User implements Cloneable {
    private String name;
    private Integer age;
    private Teacher teacher;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", User.class.getSimpleName() + "[", "]")
                .add("name='" + name + "'")
                .add("age=" + age)
                .add("teacher=" + teacher)
                .toString();
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        User user = (User) super.clone();
        //深拷贝
        //user.teacher = (Teacher) this.teacher.clone();
        return user;
    }
}

class Teacher implements Cloneable {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}